package com.buss.customer.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.controller.BaseController;
import com.buss.core.dic.RecordStatus;
import com.buss.core.model.User;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.UserService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.utils.StringUtil;
import com.buss.customer.model.Customer;
import com.buss.customer.model.CustomerClass;
import com.buss.customer.model.CustomerSource;
import com.buss.customer.service.CustomerClassService;
import com.buss.customer.service.CustomerService;
import com.buss.customer.service.CustomerSourceService;
@Controller
@RequestMapping("/admin/customer/")
public class CustomerController extends BaseController {

	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private CustomerClassService classService;
	
	@Autowired
	private CustomerSourceService sourceService;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("page")
	public ModelAndView listPage(String menuid,HttpServletRequest request) {
		ModelAndView view = new ModelAndView("customer/listpage");
		List<User> users = userService.queryUserBySales(getCompanyId(request), true).getData();
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		view.addObject("users", users);
		return view;
	}

	@RequestMapping("{type}")
	public ModelAndView editPage(@PathVariable String type,Long id,HttpServletRequest request){
		ModelAndView view = new ModelAndView("customer/edit");
		Customer customer = new Customer();
		List<CustomerSource> sources = sourceService.querySource(getCompanyId(request), null, null).getData();
		if(EDIT_PAGE.equals(type)){
			customer = customerService.loadCustomer(id);
		}else{
			customer.setStatus(RecordStatus.normal);
			customer.setIsOpenToSales(true);
			CustomerClass customerClass = classService.loadClass(id);
			customer.setCustomerClass(customerClass);
		}
		view.addObject("customerSources", sources);
		view.addObject("customer", customer);
		return view;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public Map<String, Object> add( Customer customer,HttpServletRequest request){
		customer.setCompanyId(getCompanyId(request));
		customerService.saveCustomer(customer);
		Map<String, Object> map = SysStaticParam.resultMessage("新增往来单位信息成功!");
		return map;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Map<String, Object> edit(Customer customer,HttpServletRequest request){
		customer.setCompanyId(getCompanyId(request));
		customerService.editCustomer(customer);
		return SysStaticParam.resultMessage("修改往来单位信息成功!");
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object> delete(Long[] ids){
		customerService.removeCustomer(ids);
		return SysStaticParam.resultMessage("删除往来单位信息成功!");
	}

	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> list(Long classId,String name,String mnemonic,Long salesId,
			Boolean isCustomer,Boolean isSupplier,RecordStatus status,
			String orderField,String orderType,
			@ModelAttribute Pagination page,HttpServletRequest request){
		OrderBy orderBy = null;
		if(StringUtil.isNotEmpty(orderField)){
			orderBy = new OrderBy();
			orderBy.add(orderField, orderType);
		}
		ResultList<Customer> rs = customerService.queryCustomer(getCompanyId(request), classId, name, mnemonic, salesId, 
				isCustomer, isSupplier, status, page, orderBy);
		Map<String, Object> map = SysStaticParam.resultMessage("获取往来单位信息成功!");
		map.put(SysStaticParam._RESULT, rs);
		return map;
	}

	@RequestMapping("selectcustomer")
	public ModelAndView selectCustomer(){
		ModelAndView view = new ModelAndView("customer/selectcustomer");
		return view;		
	}
	
	@Override
	public ModelAndView listPage(String menuid) {
		return null;
	}
	

}
